import BaseNet from "../framework/BaseNet";
import { Config } from "../sdk/Config";
import { GameVoManager } from "../manager/GameVoManager";
import GlobalData from "../GlobalData";
import { Util } from "../utils/Util";
import { Const } from "../config/Const";
import { Technology } from "../common/Common_Define";

export class ErrCode {
  public static code = {
    // "2001": "角色不存在",
    // "2002": "用户不存在",
    // "2010": "宝箱不存在",
    // "2011": "时间未到，无法领取宝箱",
    // "2012": "今日观看视频次数已达上限",
    // "2013": "宝箱已达到最高等级，无法升级",
    // "2014": "该道具不存在，请确认",
    // "2015": "数量不足，无法升级",
    // "2016": "无法重复升级宝箱",
    // "2017": "无法重复刷新宝箱奖励",
    // "2018": "宝箱还不能开启",
    // "2019": "宝箱尚未开启，无法刷新奖励",
    // "2020": "加速功能休息中！",
    // "2021": "不满足条件，请参与战斗获得奖杯!",
  };
}

let _instance: NetAdapter;

export default class NetAdapter {
  public static Init(): void {
    _instance = new NetAdapter();
  }

  public url = "https://jyxb-server.ruishangkeji.cn/appapi/";
  public urlBase = "https://jyxb-server.ruishangkeji.cn/";

  public static CheckPayInfo() {
    if (GameVoManager.getInstance.stateVo.isGetData) {
      let info = GameVoManager.getInstance.getOneRepairInfo();
      if (info) {
        NetAdapter.repairPayInfo({
          order_id: info.orderId,
          delivered_at: info.delivered_at,
        })
          .then((res) => {
            GameVoManager.getInstance.removeOrderInfo(info.orderId);
          })
          .catch((err) => {});
      }
    }
  }

  public static httpPost(
    request: string,
    body: any,
    customUrl: string = null,
    authorization: string = null
  ): Promise<any> {

    let auth = authorization;

    return BaseNet.httpPost(_instance.urlBase + request, body, auth);
  }

  public static httpGet(
    request: string,
    body?: any,
    customUrl: string = null,
    authorization: string = null
  ): Promise<any> {

    if (!body.uid && GlobalData.inst.uid) {
      body.uid = GlobalData.inst.uid;
    }

    let auth =  GlobalData.inst.token || "";

    return BaseNet.httpGet(_instance.url + request, body, auth);
  }

  public static httpDelete(
    request: string,
    body: any,
    customUrl: string = null,
    authorization: string = null
  ) {
    return;
  }

  public static httpPut(
    request: string,
    body: any,
    customUrl: string = null,
    authorization: string = null
  ) {
    return null; 
  }

  public static repairPayInfo(info: any): Promise<any> {
    return _instance._repairOrderInfo(info);
  }

  public static requestOrderId(): Promise<any> {
    return _instance._requestOrderId();
  }

  /** 获取  服务器时间 */
  public static getServerTime(): Promise<any> {
    return _instance._getServerTime();
  }

  /** 获取  KVDATA */
  public static getKVData(): Promise<any> {
    return _instance._getKVData();
  }

  /**
   * 上报  KVDATA
   */
  public static putKVData(params: any): Promise<any> {
    return new Promise((resolve, reject) => {
      /** 上报数据 */
      if (GameVoManager.getInstance.stateVo.isGetData) {
        return _instance
          ._putKVData(params)
          .catch(() => {
            // AlertManager.showNormalTips("网络波动，数据保存失败", 6, 2);
            reject();
          })
          .then(resolve);
      }
    });
  }

  public static patchKVData(params: any): Promise<any> {
    return _instance._patchKVData(params);
  }

  /**
   * 获取  排行榜
   * @param params type:自定义的排行榜类型, cycle:清榜周期  'week'为一周，'forever'为永久，page：请求页数，page_size:每页的数据个数
   */
  public static getRankData(params: any): Promise<any> {
    return _instance._getRankData(params);
  }

  /**
   * 上报  排行榜
   * @param params type:自定义的排行榜类型, cycle:清榜周期，'week'为一周，'forever'为永久，score：分数，customData:自定义数据，例如头像
   */
  public static postRankData(params: any): Promise<any> {
    return _instance._postRankData(params);
  }

  /**
   * 上报  登录小游戏服务器(微信静默登录时自动登录)
   */
  public static postMiniGameLogin(
    params: any,
    timeOutFunc: Function
  ): Promise<any> {
    return _instance._postMiniGameLogin(params, timeOutFunc);
  }

  /**
   * 获取  交叉推广数据
   */
  public static getDSPInfo(param: any): Promise<any> {
    return _instance._getDSPInfo(param);
  }

  public static onlineTime(time: number = 10): Promise<any> {
    return _instance._onLineTIme(time);
  }

  public static getRankInfoByKey(param: string[]): Promise<any> {
    return _instance._getRankInfoByKey(param);
  }

  public static putNickName(nick_name): Promise<any> {
    return _instance._putNickName(nick_name);
  }

  public static getRandomNickName(): Promise<any> {
    return _instance._getRandomNickName();
  }

  public static postByteDanceActiveLog(param: any): Promise<any> {
    return _instance._postByteDanceActiveLog(param);
  }

  /**
   * 获取配置  更新热更新
   * @param param service:服务名称
   */
  public static getUpdateHot(param: Record<string, any> = {}) {
    param.service = "Elite.Login.getConfig";
    return this.httpGet("", param);
  }

  /**
   * 好游快爆登录接口 废弃
   * @param param service:服务名称{
   * userid:
   * usertype:
   * nickname:
   * agent_uid:
   * }
   */
  public static getUnionidApp(param: Record<string, any> = {}) {
    // param.service = 'Elite.Login.getUnionidAppNew';
    param.service = "Elite.Login.login";
    param.userid = GlobalData.inst.userid;
    return this.httpPost("appapi/", param);
  }

  /**
   * 抖音小程序code换openid接口
   * @param param service:服务名称
   * code:
   */
  public static getOpenIDMiniDouYin(param: Record<string, any> = {}) {
    param.service = "Elite.DouYin.getOpenIDMini";
    return this.httpGet("", param);
  }

  /**
   * DouYin - 抖音小程序登录注册
   * @param param service:服务名称
   * openid: data.openid,
   * avatar: userinfo.avatarUrl || "",
   * nickname: userinfo.nickName || "",
   * agent_uid: 0
   */
  public static loginMiniDouYin(param: Record<string, any> = {}) {
    param.service = "Elite.DouYin.loginMini";
    return this.httpPost("appapi/", param);
  }

    /**
     * 快手 - 快手小游戏登录
     * @param param service:服务名称
     * code: code,
     * avatarUrl: userinfo.avatarUrl || "",
     * nickName: userinfo.nickName || "",
     * alias: 
     * agent_uid: 
     */
    public static loginKuaiShou(param: Record<string, any> = {}) {
      param.service = "Elite.KuaiShou.login";
      return this.httpGet("", param);
    }
  
  /**
   * 抖音登录APP
   * @param param service:服务名称
   * code: res.id,
   *  agent_uid: this.agent_uid,
   *  equipment_id: id,
   *  equipment_type: deviceType
   */
  public static nativeDouyinLogin(param: Record<string, any> = {}) {
    param.service = "Elite.DouYin.loginAuth";
    return this.httpPost("appapi/", param);
  }

  /**
   * 微信登录
   * @param param service:服务名称
   * code: res.id,
   *  agent_uid: this.agent_uid,
   *  equipment_id: id,
   *  equipment_type: deviceType
   */
  public static nativeWeixinLogin(param: Record<string, any> = {}) {
    param.service = "Elite.WeiXin.loginAuth";
    return this.httpPost("appapi/", param);
  }


  /**
   * 自动登录获取用户信息
   * @param param service:服务名称
   * uid: GlobalData.inst.uid,
   * @returns
   */
  public static loginGetUserInfo(param: Record<string, any> = {}) {
    param.service = "Elite.User.getUserInfo";
    param.uid_want = param.uid;
    return this.httpGet("", param);
  }

  /**
   * 上传用户信息
   * @param param service:服务名称
   * uid: GlobalData.inst.uid,
   * userinfo: GlobalData.inst.userinfo
   * @returns
   */
  public static setUserinfoPost(param: Record<string, any> = {}) {
    if (!GlobalData.inst.uid) {
      return;
    }
    param.uid = GlobalData.inst.uid;
    // console.log("上传用户信息请求参数", param)
    console.log("上传用户信息请求");
    return this.httpPost("api/elite/game/syncGameData", param);
  }

  /**
   * 绑定代理
   * @param param service:服务名称
   * uid: GlobalData.inst.uid
   * to_uid: this.agent_uid
   * 缺token
   * @returns
   */
  public static userUpdateDaili(param: Record<string, any> = {}) {
    param.service = "Elite.User.bindAgent";
    param.uid = GlobalData.inst.uid;
    return this.httpPost("appapi/", param);
  }

  /**
   * 获取下级成员
   * @param param service:服务名称
   * uid: GlobalData.inst.uid
   * 缺token
   * @returns
   */
  public static getFriendUser(param: Record<string, any> = {}) {
    param.service = "Elite.User.getSubMembers";
    param.uid = GlobalData.inst.uid;
    return this.httpGet("", param);
  }
  /**
   * 看广告生成订单
   * @param param service:服务名称
   * @returns
   */
  public static advOrderDev(param: Record<string, any> = {}) {
    param.service = "Elite.User.adOrder";
    return this.httpPost("appapi/", param);
  }

  /**
   * 获取金币充值列表
   * @param param service:服务名称
   * uid: GlobalData.inst.uid
   * @returns
   */
  public static getRechargeDiamond(param: Record<string, any> = {}) {
    param.service = "Elite.Live.getRechargeDiamond";
    param.uid = GlobalData.inst.uid;
    return this.httpGet("", param);
  }

  public static setRechargeDiamond(param: Record<string, any> = {}) {
    param.service = "Elite.Live.setRechargeDiamond";
    param.uid = GlobalData.inst.uid;
    return this.httpGet("", param);
  }

  /**
   * 绑定省份
   * @param param service:服务名称
   * uid: GlobalData.inst.uid
   * @returns
   */
  public static bindProvince(param: Record<string, any> = {}) {
    param.service = "Elite.Login.bindProvince";
    param.uid = GlobalData.inst.uid;
    return this.httpGet("", param);
  }


  /**
   * 获取玩家挑战差值
   * @param param service:服务名称
   * uid: GlobalData.inst.uid
   * @returns
   */
  public static getDiffUserInfo(param: Record<string, any> = {}) {
    param.service = "Elite.Home.getFightDiff";
    param.uid = GlobalData.inst.uid;
    return this.httpGet("", param);
  }

  /**
   * 获取广告收益明细
   * @param param service:服务名称
   * uid: GlobalData.inst.uid
   * @returns
   */
  public static userIncome(param: Record<string, any> = {}) {
    param.service = "Elite.User.getAdIncome";
    param.uid = GlobalData.inst.uid;
    return this.httpGet("", param);
  }

  /**
   * UID获取用户信息接口  Live - 获取 ToBid 广告收益
   * @param param service:服务名称
   * @returns
   */
  public static getUserProfit(param: any = {}): Promise<any> {
    // param["service"] = "Elite.Live.getUserProfitNew";
    // 待确认
    param["service"] = "Elite.Home.getToBidRevenue";
    param.uid = GlobalData.inst.uid;
    return this.httpGet("", param);
  }
  /**
   * 添加提现账号
   * @param param service:服务名称
   */
  
  public static setUserAccount(param: any = {}): Promise<any> {
    param["service"] = "Elite.User.setWithdrawalAccount";
    param.uid = GlobalData.inst.uid;

    return this.httpPost("appapi/", param);
  }
  /**
   * 获取用户提现账号
   * @param param service:服务名称
   */

  public static getUserAccountList(param: any = {}): Promise<any> {
    param["service"] = "Elite.User.getWithdrawalAccount";
    param.uid = GlobalData.inst.uid;
    return this.httpGet("", param);
  }
  /**
   * 支付宝提现接口
   * @param param service:服务名称
   */
  public static tixian(param: any = {}): Promise<any> {
    param["service"] = "Elite.Charge.withdrawAlipay";
    param.uid = GlobalData.inst.uid;
    return this.httpPost("appapi/", param);
  }

    /**
   * 汇付八方微信提现
   * @param param service:服务名称
   */
    public static withdrawHuiFu(param: any = {}): Promise<any> {
      param["service"] = "Elite.Charge.withdrawHuiFu";
      param.uid = GlobalData.inst.uid;
      return this.httpPost("appapi/", param);
    }
  /**
   * 获取提现记录
   * @param param service:服务名称
   */
  public static cashrecord(param: any = {}): Promise<any> {
    param["service"] = "Elite.Charge.getWithdrawals";
    param.uid = GlobalData.inst.uid;
    return this.httpGet("", param);
  }

  //支付宝身份验证获取验证码

  public static authenticationUser(param: any = {}): Promise<any> {
    param["service"] = "Elite.User.authenticationUser";
    param.uid = GlobalData.inst.uid;
    return this.httpGet("", param);
  }
  //支付宝身份验证

  public static authenticationCode(param: any = {}): Promise<any> {
    param["service"] = "Elite.User.authenticationCode";
    param.uid = GlobalData.inst.uid;
    return this.httpGet("", param);
  }


  public static zfbAuthentication(param: any = {}): Promise<any> {
    param["service"] = "Elite.Alipay.zfbAuthentication";
    param.uid = GlobalData.inst.uid;
    return this.httpGet("", param);
  }

  /**
   * 获取瓜分奖池用户
   */
  public static jiangchi(param: any = {}): Promise<any> {
    param["service"] = "Elite.PrizePool.getUsers";
    param.uid = GlobalData.inst.uid;
    return this.httpGet("", param);
  }

  /**
   * 瓜分奖池用户入驻接口
   * @param param service:服务名称
   * uid: GlobalData.inst.uid
   * grade:关卡；
   * to_uid: 挑战的uid
   */
  public static addPrizePool(param: any = {}): Promise<any> {
    let chapter = Util.levelToChapterId(
      GameVoManager.getInstance.myUserVo.top_level
    );
    let chapterId =
      chapter[1] == 1
        ? 1
        : chapter[1] == chapter[2]
        ? chapter[0] + 1
        : chapter[0];
    console.log("chapterId", chapterId);
    param["service"] = "Elite.PrizePool.addUser";
    param.uid = GlobalData.inst.uid;
    param.grade = chapterId + 3;
    return this.httpPost("appapi/", param);
  }

  /**
   * 阿里云二要素实名认证
   * @param param
   */
  public static aliShimingRengzheng(param: any = {}): Promise<any> {
    param["service"] = "Elite.Alipay.alyAuthentication.add";
    param.uid = GlobalData.inst.uid;
    // param.token = cc.sys.localStorage.getItem(Const.LOGIN_TOKEN) || "";
    // param.id_card_no = GlobalData.inst.id_card_no;
    // param.name = GlobalData.inst.name;
    return this.httpGet("", param);
  }

  /**
   *  苹果账户密码登录
   * @param param
   */
  public static appleLogin(param: any = {}): Promise<any> {
    param["service"] = "Elite.Login.login";
    if (Util.isIOS() && GlobalData.inst.tempHide) {
      param["user_type"] = 6;
    }else
    {
      param["user_type"] = 4;
    }

    
    return this.httpPost("appapi/", param);
  }

  /**
   * 苹果账户注册
   * @param param
   * @returns
   */
  public static appleResRegister(param: any = {}): Promise<any> {
    param["service"] = "Elite.Login.register";
    param["user_type"] = 6;

    return this.httpPost("appapi/", param);
  }

  /**
   * 排行榜挑战
   * @param param
   * @returns
   */
  public static setDiffUserInfoDev(param: any = {}): Promise<any> {
    param["service"] = "Elite.Home.fightRank";
    param.uid = GlobalData.inst.uid;
    return this.httpPost("appapi/", param);
  }
  /**
   * 获取入驻岛屿用户数据
   * @param param service:服务名称
   */
  public static moveIntoUserList(param: any = {}): Promise<any> {
    param["service"] = "Elite.Island.getUsers";
    param.uid = GlobalData.inst.uid;
    return this.httpGet("", param);
  }

  /**
   * 挑战对手占领岛屿接口
   * @param param service:服务名称
   */
  public static addMoveIntoUser(param: any = {}): Promise<any> {
    param["service"] = "Elite.Island.addUser";
    param.uid = GlobalData.inst.uid;
    return this.httpPost("appapi/", param);
  }
  /**
   * 领取金币接口
   * @param param service:服务名称
   */
  public static collectCoins(param: any = {}): Promise<any> {
    param["service"] = "Elite.Island.resetHourCoins";
    param.uid = GlobalData.inst.uid;
    return this.httpPost("appapi/", param);
  }
  /**
   * 注销账号
   * @param param service:服务名称
   */
  public static writeOff(param: any = {}): Promise<any> {
    param["service"] = "Elite.User.delUser";
    param.uid = GlobalData.inst.uid;
    return this.httpPost("appapi/", param);
  }
  /**
   * 封禁设备
   * @param param service:服务名称
   */
    public static banDevice(param: any = {}): Promise<any> {
      param["service"] = "Elite.User.banDevice";
      param.uid = GlobalData.inst.uid;
      return this.httpPost("appapi/", param);
    }
  /**
   * 瓜分奖池记录
   * @param param
   * @returns
   */
  public static prizePoolLog(param: any = {}): Promise<any> {
    param["service"] = "Elite.PrizePool.log";
    param.uid = GlobalData.inst.uid;
    return this.httpGet("", param);
  }

  /**
   * 查询用户信息
   * @param param
   * @returns
   */
  public static getUserById(param: any = {}): Promise<any> {
    param["service"] = "Elite.User.getUserInfo";

    return this.httpGet("", param);
  }

  /***************内部实现************************/

  /** 获取  服务器时间 */
  _getServerTime(): Promise<any> {
    return new Promise((resolve, reject) => {
      reject();
    });
  }

  _getKVData(): Promise<any> {
    return null; //BaseProtocol.requestByConfig('kvDataGet', {});
  }

  /**
   *
   * @param params
   */
  _patchKVData(params: any): Promise<any> {
    return null; //.requestByConfig('kvDataPatch', params);
  }

  /**
   * 上报所有  KVDATA
   */
  _putKVData(params: any): Promise<any> {
    return null; //BaseProtocol.requestByConfig('kvDataPut', params);
  }

  _getKVDataByKey(param: string[]): Promise<any> {
    return null; //BaseProtocol.requestByConfig('kvDataGetByKey', { hidden: param });
  }

  /**
   * 获取  排行榜
   * @param params type:自定义的排行榜类型, cycle:清榜周期  'week'为一周，'forever'为永久，page：请求页数，page_size:每页的数据个数
   */
  _getRankData(params: any): Promise<any> {
    return null; //BaseProtocol.requestByConfig('rankGet', params);
  }

  /**
   * 上报  排行榜
   * @param params type:自定义的排行榜类型, cycle:清榜周期，'week'为一周，'forever'为永久，score：分数，customData:自定义数据，例如头像
   */
  _postRankData(params: any): Promise<any> {
    return new Promise((resolve, reject) => {
      //reject();
    }); //BaseProtocol.requestByConfig('rankPost', params);
  }

  /**
   * 上报  登录小游戏服务器(微信静默登录时自动登录)
   */
  _postMiniGameLogin(params: any, timeOutFunc: Function): Promise<any> {
    return null; //;BaseProtocol.requestByConfig('login', params, null, false, true, timeOutFunc);
  }

  /**
   * 获取  交叉推广数据
   */
  _getDSPInfo(param: any): Promise<any> {
    return null; //BaseProtocol.requestByConfig('dspGet', param);
  }

  /**
   * 在线时长统计
   */
  _onLineTIme(time: number): Promise<any> {
    return null; //BaseProtocol.requestByConfig('online', { duration: time }, null, true, false);
  }

  /**
   * 获取排行榜排名
   */
  _getRankInfoByKey(param: string[]): Promise<any> {
    return new Promise((resolve, reject) => {
      reject();
    }); //BaseProtocol.requestByConfig('selfRank', { ranking_names: param }, null, true, false);
  }

  /**
   * 更新名字
   */
  _putNickName(nick_name: string): Promise<any> {
    return new Promise((resolve, reject) => {
      reject();
    }); //BaseProtocol.requestByConfig('nick_name', { nick_name: nick_name }, null, true, false);
  }

  _getRandomNickName(): Promise<any> {
    return null; //BaseProtocol.requestByConfig("getRandomNickName", null);
  }
  _postByteDanceActiveLog(param: any): Promise<any> {
    return null; //BaseProtocol.requestByConfig('postByteDanceActiveLog', param, null, true, false);
  }

  _requestOrderId(): Promise<any> {
    return null; //BaseProtocol.requestByConfig('getOrderId', null, null, true, false);
  }

  _repairOrderInfo(orderInfo: any): Promise<any> {
    return null; //BaseProtocol.requestByConfig('repairOrderInfo', orderInfo, null, true, false);
  }

  // clmxz登录
  public static getUserInfo(param: any = {}): Promise<any> {
    param["service"] = "Elite.User.getUserInfo";
    param.uid_want = param.uid;
    return this.httpGet("", param);
  }
  /**
   * 排行榜接口
   * @param param service:服务名称
   * @returns
   */
  public static rankingList(param: any = {}): Promise<any> {
    param["service"] = "Elite.Home.getRank";
    return this.httpGet("", param);
  }

  /**
   * 抖音小游戏/快手小游戏看广告次数
   * @param param service:服务名称
   * uid: GlobalData.inst.uid
   * @returns
   */
  public static appAdOrderByMiniGame(param: any = {}): Promise<any> {
    param.service = "Elite.User.getAd";
    // param.uid = GlobalData.inst.uid;
    return this.httpGet("", param);
  }
}
